package voloshyn.paliy.ui;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;

import javax.crypto.Cipher;

import voloshyn.paliy.arduino.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class LogonActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	setContentView(R.layout.logon_activity);
	Button gallery = (Button) findViewById(R.id.buttonGallery);
	gallery.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {
		Intent intent = new Intent(LogonActivity.this,
			GalleryActivity.class);
		startActivity(intent);
	    }
	});

    }

    public void loginToApp(View v) {
	// TODO perform login logics
	// generateKeyPair();
	Intent chooseDeviceActivity = new Intent(getApplicationContext(),
		ChooseDeviceActivity.class);
	startActivity(chooseDeviceActivity);
	finish();
    }

    // TODO implement logics
    private void generateKeyPair() {
	KeyPairGenerator keyGen;
	try {
	    keyGen = KeyPairGenerator.getInstance("RSA");
	    SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
	    keyGen.initialize(4096, random);
	    KeyPair pair = keyGen.generateKeyPair();
	    PrivateKey privKey = pair.getPrivate();
	    PublicKey pubKey = pair.getPublic();
	    Cipher cipher = Cipher.getInstance("RSA");
	    cipher.init(Cipher.ENCRYPT_MODE, pubKey);
	    String helloWorld = "HelloWorld";

	    byte[] encoded = cipher.doFinal(helloWorld.getBytes());
	    String encodedString = new String(encoded);
	    Cipher decryptCipher = Cipher.getInstance("RSA");
	    decryptCipher.init(Cipher.DECRYPT_MODE, privKey);

	    String result = new String(decryptCipher.doFinal(encoded));

	    Log.i("Log", result);
	} catch (Throwable e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}

    }
}
